package com.hzhc.controller.sysmanage;

import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.LayuiSelectTreeVo;
import com.hzhc.dto.RouterDto;
import com.hzhc.entity.SysMenu;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.SysMenuService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.SysMenuVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜单管理
 *
 * @author 功不唐捐
 * @since 2024-05-23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin")
public class SysMenuController extends BaseController {
	private final SysMenuService menuService;

	/**
	 * 菜单管理列表
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping("/getDataGredMenulist")
	public ResultUtil<List<SysMenu>> getDatagridMenuList(HttpServletRequest request, @RequestBody SysMenu datas) {
		List<SysMenu> menuListAll = menuService.findMenuListAll(datas.getName(), null);
		return ResultUtil.build(menuListAll, ResultUtilCodeEnum.SUCCESS);
	}

	/**
	 * 菜单新增
	 *
	 * @param datas
	 * @return
	 */
	@RequestMapping("/saveMenu")
	@SysTemLog(operModul = "菜单管理-菜单新增", operType = "菜单新增", operDesc = "菜单新增功能", modular = "PC")
	public ResultUtil<T> saveMenu(@RequestBody SysMenu datas) {
		return menuService.saveMenu(datas);
	}

	/**
	 * 菜单修改
	 *
	 * @param datas
	 * @return
	 */
	@RequestMapping("/updateMenu")
	@SysTemLog(operModul = "菜单管理-菜单修改", operType = "菜单修改", operDesc = "菜单修改功能", modular = "PC")
	public ResultUtil<T> updateMenu(@RequestBody SysMenu datas) {
		return menuService.updateMenu(datas);
	}

	/**
	 * 菜单删除
	 *
	 * @return
	 */
	@RequestMapping("/menuDelete/{id}")
	@SysTemLog(operModul = "菜单管理-菜单删除", operType = "菜单删除", operDesc = "菜单删除功能", modular = "PC")
	public ResultUtil<T> delMenu(@PathVariable("id") Long id) {
		return menuService.delMenu(id);
	}

	@RequestMapping("/getGrantTreeMenu/{id}")
	public List<SysMenu> getGrantTreeMenu(HttpServletRequest request, @PathVariable("id") Long id) {
		return menuService.getGrantTreeMenu(id, null, null);
	}

	@RequestMapping("/getSelectTreeMenu")
	public ResultUtil<List<LayuiSelectTreeVo>> getSelectTreeMenu(@RequestBody SysMenu sysMenu) {
		List<LayuiSelectTreeVo> selectTreeMenu = new ArrayList<>();
		if (sysMenu.getType() != null){//目录
			selectTreeMenu = menuService.getSelectMlTreeMenu(sysMenu);
		}else{
			selectTreeMenu = menuService.getSelectTreeMenu(sysMenu);
		}
		return ResultUtil.build(selectTreeMenu, ResultUtilCodeEnum.SUCCESS);
	}

	//获取目录树形菜单
	@RequestMapping("/getSelectMlTreeMenu")
	public ResultUtil<List<LayuiSelectTreeVo>> getSelectMlTreeMenu(SysMenu sysMenu) {
		List<LayuiSelectTreeVo> selectTreeMenu = menuService.getSelectMlTreeMenu(sysMenu);
		return ResultUtil.build(selectTreeMenu, ResultUtilCodeEnum.SUCCESS);
	}

	/**
	 * 获取路由信息
	 */
	@PostMapping("/getRouters")
	public ResultUtil<List<RouterDto>> getRouters(HttpServletRequest request) {
		Long userId = TokenUtil.getUserID(request);
		List<SysMenuVo> menus = menuService.selectMenuTreeByUserId(userId);
		return ResultUtil.build(menuService.buildMenus(menus), ResultUtilCodeEnum.SUCCESS);
	}

}
